package com.eagle.coders.security.beans.dao.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.eagle.coders.security.beans.RanksBean;
import com.eagle.coders.security.beans.RolesBean;
import com.eagle.coders.security.beans.UsersBean;

/**
 * 
 * @author Anees
 * 
 */

public interface UsersBeanMapper {
	
	final String SELECT_ROLES = "SELECT * FROM eagle_users U left outer join eagle_role_user UR on U.id = UR.userId " +
	"left outer join  eagle_roles R on UR.roleId = R.id where U.id = #{id}";
	
	final String SELECT_RANK = "SELECT * FROM eagle_users U left outer join eagle_rank_user UR on U.id = UR.userId " +
	"left outer join  eagle_roles R on UR.rankId = R.id where U.id = #{id}";
	
	
	@Select(value = {"SELECT * FROM eagle_users o WHERE o.username = #{username} and o.password = #{password}"})
	UsersBean findUsersByUserNamePassword(@Param(value = "username")String username, @Param(value= "password")String password);

	@Select(value = "SELECT * FROM eagle_users o WHERE o.username = #{username}")
	UsersBean findUsersByUserName(String username);
	
	@Select(value = "SELECT * FROM eagle_users")
	List<UsersBean> findAllUsers();
	
	@Select(value = "SELECT * FROM eagle_users o WHERE o.id = #{id}")
	UsersBean findUserById(Integer id);
	
	@Select(value = SELECT_ROLES)
	@Results(value = {
		@Result(property="userRoles", javaType=List.class, many=@Many)
	})
	List<RolesBean> findRolesForUser(@Param(value = "id")Integer id);
	
	@Select(value = SELECT_RANK)
	@Results(value = {
		@Result(property="userRank", javaType=List.class, many=@Many)
	})
	List<RanksBean> findRankForUser(@Param(value = "id")Integer id);
}
